﻿Imports System
Imports System.ComponentModel
Imports System.Windows.Forms
Imports System.Workflow.Activities.Rules
Imports System.Workflow.Activities.Rules.Design
Imports System.Workflow.ComponentModel.Design

Namespace System.Workflow.Activities
    <ActivityDesignerTheme(GetType(PolicyDesignerTheme))> _
    Friend NotInheritable Class PolicyDesigner
        Inherits ActivityDesigner
        Implements IServiceProvider
        ' Methods
        Protected Overrides Sub DoDefaultAction()
            MyBase.DoDefaultAction
            Dim service As WorkflowDesignerLoader = [TryCast](Me.GetService(GetType(WorkflowDesignerLoader)),WorkflowDesignerLoader)
            If ((Not service Is Nothing) AndAlso service.InDebugMode) Then
                Throw New InvalidOperationException(Messages.DebugModeEditsDisallowed)
            End If
            Dim activity As PolicyActivity = DirectCast(MyBase.Activity, PolicyActivity)
            If Not Helpers.IsActivityLocked(activity) Then
                Dim definitions As RuleDefinitions = ConditionHelper.Load_Rules_DT(Me, Helpers.GetRootActivity(activity))
                If (Not definitions Is Nothing) Then
                    Dim ruleSets As RuleSetCollection = definitions.RuleSets
                    Dim ruleSetReference As RuleSetReference = activity.RuleSetReference
                    Dim ruleSet As RuleSet = Nothing
                    Dim key As String = Nothing
                    If ((Not ruleSetReference Is Nothing) AndAlso Not String.IsNullOrEmpty(ruleSetReference.RuleSetName)) Then
                        key = ruleSetReference.RuleSetName
                        If ruleSets.Contains(key) Then
                            ruleSet = ruleSets.Item(key)
                        End If
                    Else
                        key = ruleSets.GenerateRuleSetName
                    End If
                    [Using] dialog As RuleSetDialog = New RuleSetDialog(activity, ruleSet)
                        If (DialogResult.OK = dialog.ShowDialog) Then
                            If (Not ruleSet Is Nothing) Then
                                ruleSets.Remove(key)
                            Else
                                dialog.RuleSet.Name = key
                                activity.RuleSetReference = New RuleSetReference(key)
                            End If
                            ruleSets.Add(dialog.RuleSet)
                            ConditionHelper.Flush_Rules_DT(Me, Helpers.GetRootActivity(activity))
                        End If
                    End [Using]
                End If
                TypeDescriptor.GetProperties(activity).Item("RuleSetReference").SetValue(activity, activity.RuleSetReference)
            End If
        End Sub

        Public Function GetService(ByVal type As Type) As Object
            Return MyBase.GetService(type)
        End Function

    End Class
End Namespace

